package model

import "github.com/jinzhu/gorm"

type Category struct {
	Model

	Name  string `json:"name" gorm:"size:128;unique"` // 分类名称
	State uint8  `json:"state" gorm:"index"`          // 状态 0：禁用，1启用
	Total uint   `json:"total"`                       // 关联文章数
}

func (c Category) TableName() string {
	return "category"
}

func (c *Category) List(tx *gorm.DB, limit, offset int) (categories []Category, total int, err error) {
	if err := tx.Limit(limit).Offset(offset).Find(&categories).Error; err != nil {
		return nil, 0, err
	}

	tx.Table(c.TableName()).Count(&total)

	return categories, total, nil
}

func (c *Category) Create(tx *gorm.DB) error {
	return tx.Create(c).Error
}

func (c *Category) Delete(tx *gorm.DB) error {
	return tx.Delete(c, "id = ?", c.Id).Error
}
